import java.util.*;
import java.sql.*;

public class Program {
	
	static Connection conn;

	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Welcome to the Your Institution University Course Suggester Software!");
		System.out.println("_____________________________________________________________________");
		
		Database.connectToDatabase();

		char input = 'q';
		String in = "";
		boolean go = true;
		int privelegeLevel = 0; 
		System.out.println("User type?\n0. Student\n1. Advisor");
		privelegeLevel = scan.nextInt();
		if(privelegeLevel > 1) privelegeLevel = 0;

		while (go){

			System.out.println("\n\nHello, " + (privelegeLevel < 1 ? "student" : "advisor") + ", Please select a menu option:");
			System.out.println("1. Find courses by major code");
			System.out.println("2. Find courses by text search");
			System.out.println("3. Find classes starting at a certain hour");
			System.out.println("4. Find classes by semester");
			System.out.println("5. Find all classes in a room/building");
			if(privelegeLevel > 0){
				System.out.println("6. Add Student");
				System.out.println("7. Delete Student");
				System.out.println("8. Find Student Graduation Year");
				System.out.println("9. Change Student Graduation Year");
				System.out.println("a. List students with certain GPA range");
			}

			System.out.print("\nOr enter Q to quit.\n\n> ");
			
			in = scan.next();
			input = in.charAt(0);
			
			//Note: Input is a CHAR, not an INT.
			try {
				switch(input){
				case '1':
					System.out.println("Input a major code (CHE = Chemistry, CS = Computer Science, etc.):");
					String major = scan.next();
					Database.findCoursesByMajor(major);break;
				case '2':
					System.out.println("Input a text string to search. Results, if any, will contain the exact phrase:");
					String searchText = scan.next();
					Database.findClassByName(searchText);
					break;
				case '3':
					System.out.println("Input an hour for your class to start (0 = Midnight, 23 = 11 PM):");
					int hour = scan.nextInt();
					Database.findClassesStartingAt(hour);
					break;
				case '4':
					System.out.println("Input the semester code for a class to be in (fa2012, sp2013, etc):");
					String season = scan.next();
					Database.findCoursesBySemester(season);
					break;
				case '5':
					System.out.println("Input the room number or building abbreviation (SB 3.02.10A, IP 127.0.0.1, etc):");
					String buildingID = scan.next();
					Database.findAllClassesIn(buildingID);
					break;
				case '6':
					if(privelegeLevel < 1){
						System.out.println("AHAHAHAH NOPE.");
						break;
					}
					System.out.println("Adding Student (Step 1/6) Input sid:");
					int sid = scan.nextInt();
					System.out.println("Adding Student (Step 2/6) Input student first name:");
					String fname = scan.next();
					System.out.println("Adding Student (Step 3/6) Input student last name:");
					String lname = scan.next();
					System.out.println("Adding Student (Step 4/6) Input major:");
					major = scan.next();
					System.out.println("Adding Student (Step 5/6) Input current credits:");
					int credits = scan.nextInt();
					System.out.println("Adding Student (Step 6/6) Input student's GPA:");
					double GPA = scan.nextDouble();
					Database.addStudent(sid, fname, lname, major, credits, GPA);
					break;
				case '7':
					if(privelegeLevel < 1){
						System.out.println("AHAHAHAH NOPE.");
						break;
					}
					System.out.println("Deleting student. Input sid:");
					sid = scan.nextInt();
					Database.delStudent(sid);
					break;
				case '8':
					if(privelegeLevel < 1){
						System.out.println("AHAHAHAH NOPE.");
						break;
					}
					System.out.println("Finding graduation year of a student. Input sid:");
					sid = scan.nextInt();
					Database.findStudentGradYear(sid);
					break;
				case '9':
					if(privelegeLevel < 1){
						System.out.println("AHAHAHAH NOPE.");
						break;
					}
					System.out.println("Changing student graduation year. Input sid:");
					sid = scan.nextInt();
					System.out.println("Input student's new graduation year:");
					int newYear = scan.nextInt();
					Database.changeGraduation(sid, newYear);
					break;
				case 'a':
					if(privelegeLevel < 1){
						System.out.println("AHAHAHAH NOPE.");
						break;
					}
					System.out.println("Input the lowest GPA to return: ");
					double lowest = scan.nextDouble();
					System.out.println("Input the highest GPA to return: ");
					double highest = scan.nextDouble();
					Database.listAllStudentsByGPARange(lowest, highest);
					break;
				case 'q':case 'Q':					
					go = false;
					System.out.println("\nThank you for using the Course Suggester Software!\n");
					break;
				default:System.out.println("I'm afraid I can't do that, Tim.");
				}
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		scan.close();

	}

}
